package cn.timelost.hr.controller;

import cn.timelost.hr.config.utils.JWTUtils;
import cn.timelost.hr.config.utils.SysTime;
import cn.timelost.hr.dao.AttendanceoutDao;
import cn.timelost.hr.dao.PersonalDao;
import cn.timelost.hr.enums.ResultEnum;
import cn.timelost.hr.pojo.*;
import cn.timelost.hr.service.AttendanceoutService;
import cn.timelost.hr.service.UserService;
import cn.timelost.hr.vo.ResultVo;
import cn.timelost.hr.vo.UnAttendanceoutVo;
import cn.timelost.hr.vo.input.AttendanceoutForm;
import cn.timelost.hr.vo.input.PersonalRewardForm;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.apache.ibatis.jdbc.Null;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.sql.Date;
import java.util.List;

/**
 * 签退接口
 * @auther LiuXinqi
 */
@RestController//响应返回全json
@RequestMapping("/attendanceout")
public class AttendanceoutController {

    @Resource
    AttendanceoutService attendanceoutService;
    @Resource
    AttendanceoutDao attendanceoutDao;
    @Resource
    PersonalDao personalDao;
    @Autowired
    UserService userService;



    /**
     * 正常全表=单查询功能
     */
    @GetMapping("/attendanceout/list")
    public PageInfo<Attendanceout> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                     @RequestParam(value = "personalId", defaultValue = "0") Integer personalId,
                                     @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date beginDate,
                                     @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date endDate
    ) {
        return attendanceoutService.findAll(page, size, personalId, new SysTime().getTurnDate(beginDate) , new SysTime().getTurnDate(endDate));
    }
    //    @GetMapping("/attendanceout/list")
//    public PageInfo<Attendanceout2> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
//                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
//                                     @RequestParam(value = "personalId", defaultValue = "0") Integer personalId,
//                                     @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date beginDate,
//                                     @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date endDate
//    ) {
//        System.out.println("Hello08"+personalId);
//        return attendanceoutService.findAllView(page, size, personalId, new SysTime().getTurnDate(beginDate) , new SysTime().getTurnDate(endDate));
//    }
    @GetMapping("/attendanceout/list2")
    public PageInfo<Attendanceout2> list2(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "size", defaultValue = "10") Integer size,
                                       @RequestParam(value = "personalId", defaultValue = "0") Integer personalId,
                                       @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date beginDate,
                                       @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") java.util.Date endDate
    ) {
        System.out.println();
        System.out.println("Hello081"+attendanceoutService.findUnAttendanceoutAll(page, size));
        return attendanceoutService.findAllView(page, size, personalId, new SysTime().getTurnDate(beginDate) , new SysTime().getTurnDate(endDate));
    }

    @GetMapping("/attendanceout/listUnattendanceout")
    public PageInfo<UnAttendanceoutVo> listUnattendanceout(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        return attendanceoutService.findUnAttendanceoutAll(page, size);
    }



    @GetMapping("/attendanceout/all")
    public List<Attendanceout> all() {
        return attendanceoutService.all();
    }
    @GetMapping("/attendanceout/all2")
    public List<Attendanceout2> allView() {
        return attendanceoutService.allView();
    }

    @GetMapping("/attendanceout/{id}")
    public Attendanceout findById(@PathVariable Integer id) {
        return attendanceoutService.find(id);
    }



    /**
     * 和PersonReword的find(id)返回类型一样，和PersonReword的insert(Form)参数类型类似
     * @param attendanceoutForm
     * @return
     */
    @GetMapping("/get")
//    public Attendanceout signUp(@RequestBody @Valid AttendanceoutForm attendanceoutForm){
    public Attendanceout signUp(@RequestBody @Valid AttendanceoutForm attendanceoutForm){
        if(attendanceoutForm==null)return null;
        if(attendanceoutForm.getPersonalId()==null)return null;
        System.out.println("hello09"+attendanceoutForm.getPersonalId());
        return attendanceoutService.signUp(attendanceoutForm.getPersonalId());
    }

    @PostMapping("/insert")
    public ResultVo insert(@RequestBody @Valid AttendanceoutForm attendanceoutForm) {

        Attendanceout attendanceout=attendanceoutService.signUp(attendanceoutForm.getPersonalId());
        try{
            attendanceoutDao.insert(attendanceout);
            return ResultVo.success();
        }catch (Exception e){
            return ResultVo.fail(ResultEnum.ATTENDANCE_EXIST);
        }
    }

    /**
     * insert2
     * 无参签到，但需要员工登录
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/insertAuto")
    public ResultVo insertAuto(HttpServletRequest httpServletRequest) {
        System.out.println("hello4");
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        User user=userService.findByUsername(username);
        Date date= new SysTime().getDate();

        try{
            System.out.println("Hello4"+user.getId());
            Attendanceout attendanceout=attendanceoutDao.getStatusInt(user.getId(),date);
            System.out.println("Hello4"+attendanceout.getStatus());
//            return attendanceout.getStatus();
        }catch (Exception e){
            System.out.println("Hello4"+"-1");
//            return -1;//没查到
        }
//        String token = httpServletRequest.getHeader("Authorization");
//        String username = JWTUtils.getUsername(token);
//        User user=userService.findByUsername(username);

//        Attendanceout attendanceout=attendanceoutService.signUp(user.getId());
        System.out.println("signat"+user.getUsername()+" "+personalDao.findAllByName(user.getUsername()));

        try{
            attendanceoutService.signUp(personalDao.findAllByName(user.getUsername()));
            return ResultVo.success();
        }catch (Exception e){
            return ResultVo.fail(ResultEnum.ATTENDANCE_EXIST);
        }
    }
    /**
     * insertUn
     * 补签，需要传入员工id，但需要在未签到表单使用
     * @param id
     * @return
     */
    @PostMapping("/insertUn")
    @RequiresRoles("admin")
    public ResultVo insertUn(@PathVariable Integer id) {
//        System.out.println("hello4");
//        String token = httpServletRequest.getHeader("Authorization");
//        String username = JWTUtils.getUsername(token);
//        User user=userService.findByUsername(username);
        Date date= new SysTime().getDate();

        try{
            System.out.println("Hello4"+id);
            Attendanceout attendanceout=attendanceoutDao.getStatusInt(id,date);
            System.out.println("Hello4"+attendanceout.getStatus());
//            return attendanceout.getStatus();
        }catch (Exception e){
            System.out.println("Hello4"+"-1");
//            return -1;//没查到
        }
//        String token = httpServletRequest.getHeader("Authorization");
//        String username = JWTUtils.getUsername(token);
//        User user=userService.findByUsername(username);
//        Attendanceout attendanceout=attendanceoutService.signUp(user.getId());
//        System.out.println("signat"+user.getUsername()+" "+personalDao.findAllByName(user.getUsername()));

        try{
            attendanceoutService.signUpUn(id);
            return ResultVo.success();
        }catch (Exception e){
            return ResultVo.fail(ResultEnum.ATTENDANCE_EXIST);
        }
    }

    /**
     * 获取签到情况
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/getAttendanceoutStatus")
    public int findStatus(HttpServletRequest httpServletRequest){

        //测试其它功能
        System.out.println("HelloW"+attendanceoutDao.getAttendanceout0());
        System.out.println("HelloW"+attendanceoutDao.getAttendanceout1());
        System.out.println("HelloW"+attendanceoutDao.getAttendanceout2());
        System.out.println("HelloW"+attendanceoutDao.getAttendanceoutS());

        //
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        User user=userService.findByUsername(username);
        Date date= new SysTime().getDate();
        System.out.println("HelloW1"+personalDao.findAllByName(username));
        try{
            Attendanceout attendanceout=attendanceoutDao.getStatusInt(personalDao.findAllByName(username),date);
            System.out.println("Hello40"+attendanceout.getStatus());
            return attendanceout.getStatus();
        }catch (Exception e){
            return -1;//没查到
        }
    }
    /**
     * 获取今日签到各个情况人数,0已经签到1迟到2旷工3未签
     */
    @GetMapping("/getAttendanceoutStatus0")
    public int getAttendanceout0(HttpServletRequest httpServletRequest){
        return attendanceoutDao.getAttendanceout0();
    }
    @GetMapping("/getAttendanceoutStatus1")
    public int getAttendanceout1(HttpServletRequest httpServletRequest){
        return attendanceoutDao.getAttendanceout1();
    }
    @GetMapping("/getAttendanceoutStatus2")
    public int getAttendanceout2(HttpServletRequest httpServletRequest){
        return attendanceoutDao.getAttendanceout2();
    }
    @GetMapping("/getAttendanceoutStatusS")
    public int getAttendanceoutS(HttpServletRequest httpServletRequest){
        return attendanceoutDao.getAttendanceoutS();
    }

    @GetMapping("/getAttendanceoutStatusAllnum")
    public int[] getAttendanceoutAllnum(HttpServletRequest httpServletRequest){
        int num[]=new int[4];
        num[0]=attendanceoutDao.getAttendanceout0();;
        num[1]=attendanceoutDao.getAttendanceout1();;
        num[2]=attendanceoutDao.getAttendanceout2();;
        num[3]=attendanceoutDao.getAttendanceoutS();;
        return num;
    }

}
